package poc.android.weather;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ReviewCriteriaActivity extends Activity {
    
	CuisineReviewApplication application=null;
	Builder dialogBuilder=null;
	
	/**
	 * {@inheritDoc}
	 * creation / setup for Activity
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Button aGetReviewsBtn=null;
    	
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.reviewcriteria);
        
        // 0) setup the parts
        //this.application=(CuisineReviewApplication) this.getApplication();
        
        // 1) get back the button instance
        aGetReviewsBtn=(Button) this.findViewById(R.id.btnGetReviews);
        if (aGetReviewsBtn!=null) {
        	aGetReviewsBtn.setOnClickListener(new OnClickListener() {

				//@Override
				public void onClick(View v) {
					doGetReviews();
				}
			});
        }
    }
    
    /**
     * method getting review(s); could be hard-code or from the internet
     */
    private void doGetReviews() {
    	Intent aIntent=null;
    	
    	this.getDialogBuilder().setTitle("Inside doGetReviews...").
    		setMessage("Yo~ getting Reviews now... Redirecting to next page...");
    	//this.getDialogBuilder().show();
    	
    	/*
    	 * update the Application Object
    	 */
    	this.application.setReviewLocation(
			((EditText) this.findViewById(R.id.reviewCriteriaLocation)).
			getText().toString());
    	this.application.setReviewCuisine(
			((EditText) this.findViewById(R.id.reviewCriteriaCuisine)).
			getText().toString());
    	/*
    	 * create Intent and passed on the screen/activity flow to the 
    	 * next intent
    	 */
    	aIntent=new Intent(CuisineReviewApplication.INTENT_ACTION_VIEW_LIST);
    	this.startActivity(aIntent);
    }

    /**
     * helper method to create the AlertDialog.Builder(context)
     * for creating the alert(s)<p/>
     * <b>PS</b>. Similar to findViewById(int)... you can't create the 
     * Builder before the Context is setup successfully... (weird)
     * @return
     */
    private Builder getDialogBuilder() {
    	if (this.dialogBuilder==null) {
    		this.dialogBuilder=new AlertDialog.Builder(this);
    	}
    	return this.dialogBuilder;
    }
    
    /**
     * create a Menu Button for android application
     */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem aItem=null;
		super.onCreateOptionsMenu(menu);
		
		aItem=menu.add(0, CuisineReviewApplication.MENU_GET_REVIEWS, 0, R.string.review_criteria_get_reviews);
		aItem.setIcon(android.R.drawable.ic_menu_more);
		return true;
	}

	/**
	 * the listener for the Menu Button(s) created above
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case CuisineReviewApplication.MENU_GET_REVIEWS:
			this.doGetReviews();
			return true;
		}		
		return super.onMenuItemSelected(featureId, item);
	}
    
    
    
}